/**
 * dbms-sjtu
 * RecordManager.java created at Jul 17, 2011
 */
package dbms.record;

import java.util.Iterator;
import java.util.Map.Entry;

import dbms.Constants;
import dbms.absyn.ValueList;
import dbms.disk.Page;
import dbms.metadata.MasterPage;
import dbms.server.DbmsServer;

/**
 * @author weapon
 *
 */
public class RecordManager {
	
	public static final int FIELD_LENGTH = 30;

	public static final int FIELDLENGTH_LIMIT = 18;
	
	/**
	 * alloc a new page for the new TableInfo object, then write it to database file.
	 * 
	 * @param tableName
	 * @param tableSchema
	 * @return <b> the new TableInfo object </b>
	 */
	/*public TableInfo createTable(String tableName, Schema tableSchema){
		TableInfo newTable = new TableInfo(tableName, tableSchema);
		Page page = new Page();
		if(tableSchema.getInfo().size() > Constants.FIELDNUM_LIMIT){
			System.out.println("This table '" + tableName + "' has too much fields(uplimit is 34).");
			return null;
		}
		Iterator<Entry<String, FieldInfo>> iterator = tableSchema.getInfo().entrySet().iterator();
		int count = 0;
		while(iterator.hasNext()){
			Entry<String, FieldInfo> entry = iterator.next();
			if(entry.getKey().getBytes().length > FIELDLENGTH_LIMIT ){
				System.out.println("This field name '" + entry.getKey() + "' is too long. The above is 12.");
				return null;
			}
			page.setString(count * FIELD_LENGTH, entry.getKey());
			page.setInt(count * FIELD_LENGTH + FIELDLENGTH_LIMIT, entry.getValue().getType());
			page.setInt(count * FIELD_LENGTH + FIELDLENGTH_LIMIT + 4, entry.getValue().getLength());
			count++;
		}
		newTable.setFirstPage(page.append());
		
		
		registerInMasterTable(newTable, null);  //TODO
		
		return newTable;
	}*/
	
	/**
	 * alloc a new page for the new TableInfo object, then write it to database file.
	 * @param tableName
	 * @param tableSchema
	 */
	/*public static void createTable(String tableName, Schema tableSchema){
		Page page = new Page();
		TableInfo newTable = new TableInfo(tableName, tableSchema);
		RecordPage recordPage = new RecordPage(page, newTable, 0);
		recordPage.format(page);
		newTable.setFirstPage(DbmsServer.masterPage.recordpage.recordNum + 1);
		DbmsServer.masterPage.insert(newTable);
		DbmsServer.masterPage.writeToDisk();
		recordPage.page.append();
	}*/
	
	/**
	 * 1. delete this table's record in sqlsjk_master table;
	 * 2. add this table's root_page_number to free_page_list;
	 * 3. when user exit, we delete this page, and truncate database file size. 
	 * @param tableName
	 * @return 0 if success
	 */
	public int deleteTable(String tableName){
		return 0;
	}
	
	/**
	 * you should register the new table to the sqlsjk_master table;
	 * it equals to insert a new record to sqlsjk_master table. </br>
	 * SQL: insert into sqlsjk_master(type, name, tbl_name, root_page_number, SQL_statement) values() 
	 */
	public int registerInMasterTable(TableInfo userTable, TableInfo masterTable){
		return 0;
	}
	
	/**
	 * SQL: delete from sqlsjk_master where name=tableName
	 */
	public int unregisterFromMasterTable(TableInfo userTable, TableInfo masterTable){
		return 0;
	}
	
	
	/**
	 * we can get the position of this table in database file from its root_page_number
	 * @param table
	 * @param values : a valueList of this table's record 
	 * @return 0 if success
	 */
	public int insertRecord(TableInfo table, ValueList values){
		
		return 0;
	}
	
	public int deleteRecord(TableInfo table, int index){
		return 0;
	}
}
